package com.xmut.mall.product.service.impl;

import com.xmut.mall.product.entity.AttrEntity;
import com.xmut.mall.product.entity.CategoryEntity;
import com.xmut.mall.product.service.AttrService;
import com.xmut.mall.product.service.CategoryService;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.AttrGroupRelationVo;
import com.xmut.mall.product.vo.GroupAttrRelationVo;
import com.xmut.mall.product.vo.addToGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.Query;

import com.xmut.mall.product.dao.AttrAttrgroupRelationDao;
import com.xmut.mall.product.entity.AttrAttrgroupRelationEntity;
import com.xmut.mall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    /**
     * 根据分组的id获取当前分组的属性关联信息
     * @param groupId
     * @return
     */
    @Override
    public RespBean getAttrGroupRelationByGroupId(Long groupId) {
        // 先获取全部关联
        List<AttrAttrgroupRelationEntity> attrGroupIdList = this.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", groupId));

        // 转化为vo
        List<GroupAttrRelationVo> result = attrGroupIdList.stream().map(item -> {
            System.out.println("item:"+item);
            GroupAttrRelationVo vo = new GroupAttrRelationVo();
            vo.setId(item.getId());
            vo.setGroupId(groupId);

            AttrEntity attr = attrService.getById(item.getAttrId());
            System.out.println("attr:"+attr);
            vo.setAttrId(attr.getAttrId());
            vo.setAttrName(attr.getAttrName());

            CategoryEntity category = categoryService.getById(attr.getCatelogId());
            vo.setCateId(category.getCatId());
            vo.setCateLogName(category.getCatId()+"/"+category.getName());
            return vo;
        }).collect(Collectors.toList());
        return RespBean.success("获取成功",result);
    }

    /**
     * 根据当前分组的id获取未被当前分组所关联的属性
     * @param id
     * @return
     */
    @Override
    public RespBean getNoRelationAttrByGroupId(Long id) {
        // 再查出当前分组和attr的关联信息;
        List<AttrAttrgroupRelationEntity> attrGroupList = this.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", id));
        // 收集被当前分组关联了的attr的id
        List<Long> attrGroupRelationList = attrGroupList.stream().map(AttrAttrgroupRelationEntity::getAttrId)
                .collect(Collectors.toList());
        // 使用notin 把除了这些被关联了的attr查出来
        List<AttrEntity> attrList = attrService.list(new QueryWrapper<AttrEntity>().notIn("attr_id", attrGroupRelationList));
        return RespBean.success("获取成功",attrList);
    }

    /**
     * 添加分组的关联属性
     * @param vo
     * @return
     */
    @Override
    public RespBean addToGroup(addToGroupVo vo) {
        // 组装数据
        List<AttrAttrgroupRelationEntity> result = vo.getAttrIds().stream().map(item -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrId(item);
            entity.setAttrGroupId(vo.getGroupId());
            entity.setAttrSort(0);
            return entity;
        }).collect(Collectors.toList());

        // 插入数据库
        boolean b = this.saveBatch(result);
        if (b){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }

    }

    /**
     * 根据表id移除关联关系
     * @param id
     * @return
     */
    @Override
    public RespBean removeRelation(Long id) {
        boolean b = this.removeById(id);
        if (b){
            return RespBean.success("移除成功");
        }else {
            return RespBean.error("移除失败");
        }

    }

}